package com.miszczu.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.miszczu.model.Grades;
import com.miszczu.utils.CustomHibernateSupport;

@Repository
public class GradesDaoImpl extends CustomHibernateSupport implements GradesDao {

	public List<Object[]> getGradesForClass(Integer classId, Integer subjectId) {

		List<Object[]> grades = getSession().createSQLQuery("SELECT `g`.`grade_value`, `g`.`grade_description`, `g`.`user_id`, `g`.`grade_id` FROM `grades` as `g` JOIN `usersclasses` as `u` ON `g`.`user_id` = `u`.`user_id` " 
					+"LEFT JOIN `users` as `us` ON `us`.`user_id` = `g`.`user_id` WHERE `u`.`class_id` = :classId AND `g`.`subject_id` = :subjectId ORDER BY `us`.`last_name` ASC, `g`.`user_id`")
					.setInteger("classId", classId)
					.setInteger("subjectId", subjectId)
					.list();
		
		return grades;
	}

	public void save(Grades grade) {
		
		getHibernateTemplate().save(grade);
		
		
	}

	public void delete(Grades grade) {

		getHibernateTemplate().delete(grade);
		
	}

	public Grades getOne(Integer id) {

		return getHibernateTemplate().get(Grades.class, id);
		
	}

}
